import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController,Events} from 'ionic-angular';
import * as moment from 'moment';

import { ProgressSearch} from '../../../../model/pages/client/client-note/client-note-list/client-note-list.model';
import {DateService} from '../../../../services/common/date.service';
import {ToastService} from '../../../../services/common/toast.service';

@IonicPage()
@Component({
    selector: 'page-client-note-filter',
    templateUrl: 'client-note-filter.html',
    providers:[
        DateService,
        ToastService
    ]
})
export class ClientNoteFilterPage {
    // 时间插件--年月日汉字填充
    private pickerOptions = {
        cssClass: 'filter-add-picker'
    };
    // 筛选条件
    private filterData:ProgressSearch=new ProgressSearch();
    // 跟进类型组合
    private typeArr:number[]=[];
    // 开始时间
    private from:string;
    // 结束时间
    private to:string;
    // 提交拦截
    private canSubmit:boolean=true;
    // 年度列表
    private yearList:any[]=[];

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public toastCtrl:ToastController,
                public dateService:DateService,
                public toastService:ToastService,
                public event:Events
                ) {
        this.yearList=this.dateService.getYearList(2010);
    }

    public backButtonHandle = () => this.backClickHandle();

    ionViewDidLoad() {
        this.filterData = JSON.parse(JSON.stringify(this.navParams.get('filterData')));
        if(this.filterData.noteTypes&&this.filterData.noteTypes.length>0){
            this.typeArr=this.filterData.noteTypes
        }
        this.filterData.starttime?this.from=moment(new Date(this.filterData.starttime*1000+8*60*60*1000)).toISOString():null;
        this.filterData.endtime?this.to=moment(new Date((this.filterData.endtime)*1000+8*60*60*1000-(23*60*60+59*60+59)*1000)).toISOString():null;
    }

    // 返回
    backClickHandle() {
        this.navCtrl.pop()
    }

    // 类型选择
    selectType(num,arr){
        let status=false;
        for (let a=0;a<arr.length;a++){
            if(arr[a]==num){
                status=true;
                arr.splice(a,1)
            }
        }
        if(!status){
            arr.push(num)
        }
    }

    // 检测类型包含
    testType(num,arr){
        if(arr.length>0){
            return arr.join(',').indexOf(num)>-1;
        }
    }

    // 时间选择
    monthSelect(e,type){
        if(type==1){
            this.filterData.starttime=moment(new Date(moment(new Date(this.from)).year(),moment(new Date(this.from)).month(),moment(new Date(this.from)).date())).unix();
        }else{
            console.log(moment(new Date(this.to)).date());
            this.filterData.endtime=moment(new Date(moment(new Date(this.to)).year(),moment(new Date(this.to)).month(),moment(new Date(this.to)).date())).unix()+23*60*60+59*60+59;
        }

        if((this.filterData.starttime&&this.filterData.endtime)&&(this.filterData.endtime<this.filterData.starttime)){
            this.toastService.showToast('时间范围输入有误');
            this.canSubmit=false;
        }else{
            this.canSubmit=true;
        }
    }

    // 清除所选时间
    clearTime(){
        this.from='';
        this.to='';
        this.filterData.starttime=null;
        this.filterData.endtime=null;
    }

    // 提交
    submitData(){
        /*if((this.filterData.starttime&&this.filterData.endtime)||(!this.filterData.starttime&&!this.filterData.endtime)){
            this.filterData.noteTypes=this.typeArr;
            console.log(this.filterData)
            this.navParams.get('callback')(this.filterData).then(()=>this.navCtrl.pop());
            this.canSubmit=true;
        }else{
            this.toastService.showToast('时间范围输入有误','bottom');
            this.canSubmit=false;
        }*/
        this.filterData.noteTypes=this.typeArr;
        this.navParams.get('callback')(this.filterData).then(()=>this.navCtrl.pop());
        this.canSubmit=true;
        this.event.publish('progress search');
    }

    // 重置
    resetData(){
        this.typeArr=[];
        this.from='';
        this.to='';
        this.filterData.starttime=null;
        this.filterData.endtime=null;
    }
}
